//
//  LYCMarketViewController.m
//  爱鲜蜂
//
//  Created by 廖易诚 on 16/6/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "LYCMarketViewController.h"
#import "LYCMarketMenuView.h"
#import "LYCNetworkTool.h"
#import "MarketMenuModel.h"
#import "LYCProductListView.h"
#import "MarketProductModel.h"

#import "ProgressShade.h"

@interface LYCMarketViewController ()
{
    NSArray *_menuDatas;
    LYCMarketMenuView *_menuView;
    NSDictionary *_products;
    LYCProductListView *_productListView;
    NSMutableArray *_productList;
}

@end

@implementation LYCMarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self initData];
    [self setupUI];
    [self requestData];
    
}

#pragma mark - 初始化数据
- (void)initData {
    _menuDatas = [NSArray array];
    _products = [NSDictionary dictionary];
    _productList = [NSMutableArray array];
}

#pragma mark - 设置界面
- (void)setupUI {
    _menuView = [[LYCMarketMenuView alloc] initWithFrame:CGRectMake(0, 64, 100, self.view.h-64)];
    _menuView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_menuView];
    
    _productListView = [[LYCProductListView alloc] initWithFrame:CGRectMake(_menuView.w, 64, self.view.w-_menuView.w, self.view.h) style:UITableViewStyleGrouped];
    [self.view addSubview:_productListView];
    _menuView.changeRightViewDataSourceBlock = ^(id model){
        MarketMenuModel *menuModel = (MarketMenuModel *)model;
        NSInteger index = [_menuDatas indexOfObject:menuModel];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:index];
        [_productListView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    };
}

#pragma mark - 请求数据
- (void)requestData {
    // http://iosapi.itcast.cn/loveBeen/supermarket.json.php
    
    // 创建加载控件

    [[ProgressShade shade] showMessage:@"loading" addToView:self.view];
    [[ProgressShade shade] setDimBackground:YES];
    [[LYCNetworkTool shareTool] requestMethod:POST urlString:@"http://iosapi.itcast.cn/loveBeen/supermarket.json.php" params:@{@"call":@(5)} progress:^(NSProgress *downloadProgress) {
        
        NSLog(@"downloadProgress.completedUnitCount = %lld",downloadProgress.completedUnitCount);
        NSLog(@"totalUnitCount = %lld",downloadProgress.totalUnitCount);
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        //
        [[ProgressShade shade] hiddenShade];
        NSLog(@"supermarket ---> json.php.responseObject = %@",responseObject);
        NSDictionary *data = responseObject[@"data"];
        NSArray *categories = data[@"categories"];
        _menuDatas = [MarketMenuModel getDatasWithDictArray:categories];
        _menuView.menus = _menuDatas; // 赋值
        
        // 处理右侧tableView展示数据
        _products = data[@"products"];
        for (MarketMenuModel *model in _menuDatas) {
            NSArray *productDictArr = [_products objectForKey:model.ID];
            NSArray *productArr = [MarketProductModel getDatasWithDictArray:productDictArr];
            [_productList addObject:productArr];
        }
        _productListView.products = _productList;
        _productListView.menus = _menuDatas;
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
